package client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class EchoClient {
	
	private static final String IP_ADDRESS = "127.0.0.1";
	private static final int SERVER_SOCKET = 6969;

	public static void main(String[] args) {
		try {
			Socket socket = new Socket(IP_ADDRESS, SERVER_SOCKET);
			BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
			DataOutputStream toServer = new DataOutputStream(socket.getOutputStream());
			BufferedReader fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			FileWriter responseFile = new FileWriter("response_log_" + System.currentTimeMillis() +".txt");

			String userInput, response;
			while(true) {
				System.out.print(">");
				userInput = console.readLine();
				if(userInput.equalsIgnoreCase(":q")) {
					break;
				} else {
					// changed from bytes to UTF - only way to get it to behave
					toServer.writeUTF(userInput + '\n');
					response = fromServer.readLine();
					System.out.println("Server; "  + response);
					responseFile.write(response + '\n');
				}
			}
			socket.close();
			responseFile.close();
			
		} catch (IOException ioe) {
			System.err.println(ioe);
		}
	}
}
